package com.smart.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.smart.model.domain.ProjectWeeklyItemDO;
import com.smart.model.request.ProjectWeeklyItemPageRequest;
import com.smart.model.request.ProjectWeeklyItemRequest;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

/**
 * @author zhanghong
 */
@Repository
public interface ProjectWeeklyItemMapper extends BaseMapper<ProjectWeeklyItemDO> {
    ProjectWeeklyItemDO selectDOById(Long id);
    List<ProjectWeeklyItemDO> selectDOList(ProjectWeeklyItemDO projectWeeklyItem);
    int insertDO(ProjectWeeklyItemDO projectWeeklyItem);
    int batchInsertDO(List<ProjectWeeklyItemDO> projectWeeklyItemDOList);
    int updateDO(ProjectWeeklyItemDO projectWeeklyItem);
    int deleteDOById(Long id);
    int deleteDOByIds(String[] ids);

    @Select({"<script>",
            "   select count(1) from project_weekly_item",
            "   <where>",
            "       deleted=0",
            "       <if test=\"1==0\">and id=#{param.id}</if>",
            "   </where>",
            "</script>"})
    long pageCount(@Param("param") ProjectWeeklyItemRequest param);
    @Select({"<script>",
            "   select * from project_weekly_item",
            "   <where>",
            "       deleted=0",
            "       <if test=\"1==0\">and id=#{param.id}</if>",
            "   </where>",
            "   order by create_time desc limit #{limit} offset #{offset}",
            "</script>"})
    List<ProjectWeeklyItemDO> pageQuery(@Param("param") ProjectWeeklyItemRequest param, @Param("limit")  int limit, @Param("offset") long offset);

    List<ProjectWeeklyItemDO> listByRangeWeek(@Param("beginOfWeek") Date beginOfWeek, @Param("endOfWeek") Date endOfWeek);

    @Select({"<script>",
            "   select count(1) from project_weekly_item",
            "   <where>",
            "       deleted=0",
            "       <if test=\"param.startTime!=null and param.endTime!=null\">",
            "           <![CDATA[",
            "           AND ((start_time BETWEEN #{param.startTime} AND #{param.endTime})",
            "               OR (end_time BETWEEN #{param.startTime}  AND #{param.endTime})",
            "               OR (start_time <= #{param.startTime} AND end_time >= #{param.endTime})",
            "               OR (start_time >= #{param.startTime} AND end_time <= #{param.endTime}))",
            "           ]]>",
            "       </if>",
            "   </where>",
            "</script>"})
    long pageItemCount(@Param("param") ProjectWeeklyItemPageRequest request);

    @Select({"<script>",
            "   select * from project_weekly_item",
            "   <where>",
            "       deleted=0",
            "       <if test=\"param.startTime!=null and param.endTime!=null\">",
            "           <![CDATA[",
            "           AND ((start_time BETWEEN #{param.startTime} AND #{param.endTime})",
            "               OR (end_time BETWEEN #{param.startTime}  AND #{param.endTime})",
            "               OR (start_time <= #{param.startTime} AND end_time >= #{param.endTime})",
            "               OR (start_time >= #{param.startTime} AND end_time <= #{param.endTime}))",
            "           ]]>",
            "       </if>",
            "   </where>",
            "   <if test=\"param.startTime!=null and param.endTime!=null\">order by sort_num asc, create_time desc </if>",
            "   <if test=\"param.startTime==null or param.endTime==null\">order by create_time desc</if>",
            "   limit #{limit} offset #{offset}",
            "</script>"})
    List<ProjectWeeklyItemDO> pageItemQuery(@Param("param") ProjectWeeklyItemPageRequest request, @Param("limit") Integer limit, @Param("offset") long offset);

    @Select({"<script>",
            "   select * from project_weekly_item",
            "   <where>",
            "       deleted=0",
            "       <if test=\"param.startTime!=null and param.endTime!=null\">",
            "           <![CDATA[",
            "           AND ((start_time BETWEEN #{param.startTime} AND #{param.endTime})",
            "               OR (end_time BETWEEN #{param.startTime}  AND #{param.endTime})",
            "               OR (start_time <= #{param.startTime} AND end_time >= #{param.endTime})",
            "               OR (start_time >= #{param.startTime} AND end_time <= #{param.endTime}))",
            "           ]]>",
            "       </if>",
            "   </where>",
            "   <if test=\"param.startTime!=null and param.endTime!=null\">order by sort_num asc, create_time desc </if>",
            "   <if test=\"param.startTime==null or param.endTime==null\">order by create_time desc</if>",
            "</script>"})
    List<ProjectWeeklyItemDO> listItemQuery(@Param("param") ProjectWeeklyItemPageRequest request);


    List<ProjectWeeklyItemDO> queryWeeklyReportItemByWeeklyId(Long weeklyTaskId);

    Integer countOver(@Param("id") Long id, @Param("startTime") Date startTime, @Param("endTime") Date endTime);
}
